package driver

import (
	v1 "gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/fuel_type"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/transmission_type"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/vehicle"

	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/vehicle_brand"

	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/vehicle_model"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/policies"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/requests"
	"github.com/gofiber/fiber/v2"
	"github.com/spf13/cast"
)

type VehiclesController struct {
	v1.BaseController
}

func (controller *VehiclesController) Index(c *fiber.Ctx) error {
	vehicles := vehicle.GetDriverVehicles(controller.Auth(c))
	return controller.Json(c, vehicles)
}

func (controller *VehiclesController) Show(c *fiber.Ctx) error {
	vehicleModel := vehicle.GetDriverVehicle(cast.ToUint64(c.Params("id")))
	if vehicleModel.ID == 0 {
		return controller.NotFound(c)
	}
	if !policies.CanViewVehicle(controller.Auth(c), vehicleModel) {
		return controller.Forbidden(c)
	}

	return controller.Json(c, vehicleModel)
}

// / 添加车辆
func (controller *VehiclesController) Store(c *fiber.Ctx) error {

	request := new(requests.VehicleRequest)

	if errors := requests.ValidateVehicle(c, request); len(errors) > 0 {
		return controller.ValidationError(c, errors)
	}

	fuel := fuel_type.Get(request.FuelId)

	if fuel.ID <= 0 {
		return controller.Faield(c, "燃油类型错误")
	}

	transmission := transmission_type.Get(request.TransmissionId)

	if transmission.ID <= 0 {
		return controller.Faield(c, "变速器类型错误")
	}

	/// 校验品牌ID和型号ID是否存在
	brand := vehicle_brand.Get(request.BrandId)

	if brand.ID <= 0 {
		return controller.Faield(c, "品牌错误")
	}

	model := vehicle_model.Get(request.ModelId)

	if model.ID <= 0 {
		return controller.Faield(c, "型号错误")
	}

	if model.BrandId != request.BrandId {
		return controller.Faield(c, "品牌型号不匹配")
	}

	/// 检查车牌号是否存在
	if vehicle.GetByLicensePlate(request.LicensePlate).ID > 0 {
		return controller.Faield(c, "车牌号已经存在")
	}

	vehicleModel := vehicle.Vehicle{
		BrandId:        request.BrandId,                 //车辆品牌
		ModelId:        request.ModelId,                 //车辆型号
		Year:           controller.ToTime(request.Year), //车辆出厂日期
		Color:          request.Color,                   //颜色
		LicensePlate:   request.LicensePlate,            //车牌号
		Mileage:        request.Mileage,                 //行驶公里数
		FuelId:         request.FuelId,                  //燃油类型
		TransmissionId: request.TransmissionId,          //变速器类型
		EngineSize:     request.EngineSize,              //发动机排量
		Seats:          request.Seats,
		UserId:         controller.Auth(c),
	}

	vehicleModel.Create()
	if vehicleModel.ID > 0 {
		return controller.Success(c)
	} else {
		return controller.Faield(c, "创建失败")
	}
}

func (controller *VehiclesController) Update(c *fiber.Ctx) error {

	vehicleModel := vehicle.Get(cast.ToUint64(c.Params("id")))
	if vehicleModel.ID == 0 {
		return controller.NotFound(c)
	}

	userId := controller.Auth(c)
	if ok := policies.CanModifyVehicle(userId, vehicleModel); !ok {
		return controller.Forbidden(c)
	}

	request := new(requests.VehicleRequest)

	if errors := requests.ValidateVehicle(c, request); len(errors) > 0 {
		return controller.ValidationError(c, errors)
	}

	vehicleModel.BrandId = request.BrandId               //车辆品牌
	vehicleModel.ModelId = request.ModelId               //车辆型号
	vehicleModel.Year = controller.ToTime(request.Year)  //车辆出厂日期
	vehicleModel.Color = request.Color                   //颜色
	vehicleModel.LicensePlate = request.LicensePlate     //车牌号
	vehicleModel.Mileage = request.Mileage               //行驶公里数
	vehicleModel.FuelId = request.FuelId                 //燃油类型
	vehicleModel.TransmissionId = request.TransmissionId //变速器类型
	vehicleModel.EngineSize = request.EngineSize         //发动机排量
	vehicleModel.Seats = request.Seats

	rowsAffected := vehicleModel.Save()
	if rowsAffected > 0 {
		return controller.Success(c)
	} else {
		return controller.Faield(c, "更新失败")
	}
}

func (controller *VehiclesController) Delete(c *fiber.Ctx) error {

	vehicleModel := vehicle.Get(cast.ToUint64(c.Params("id")))
	if vehicleModel.ID == 0 {
		return controller.NotFound(c)
	}

	userId := controller.Auth(c)

	if ok := policies.CanDeleteVehicle(userId, vehicleModel); !ok {
		return controller.Forbidden(c)
	}

	rowsAffected := vehicleModel.Delete()

	if rowsAffected > 0 {
		return controller.Success(c)
	}

	return controller.Faield(c, "删除失败")
}
